package com.glela.platform.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glela.api.sms.xinxi1.SendMsg;
import com.glela.common.model.sms.SMSVerifyCode;
import com.glela.common.service.SMSService;
import com.glela.common.util.RandomUtil;
import com.glela.constant.CodeTimeOutConstant;
import com.glela.platform.mapper.CheckCodeMapper;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.CheckCode;
import com.glela.platform.model.User;
import com.glela.platform.service.CheckCodeService;
import com.glela.platform.util.DateUtils;

@Service("checkCodeService")
public class CheckCodeServiceImpl implements CheckCodeService
{
	private Logger logger = LoggerFactory.getLogger(CheckCodeServiceImpl.class);

	@Autowired
	private SMSService		smsService;
	@Resource
	private CheckCodeMapper checkCodeMapper;
	@Resource
	private UserMapper userMapper;

	//private SendMsg sendMsg;

	/**
	 * 校检code 超时移除
	 *
	 * @param code
	 * @param mobilePhone
	 * @return
	 */
	@Override
	public int removeCheckCode(String mobilePhone, String code, int type)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobilePhone", mobilePhone);
		map.put("type", type);
		List<CheckCode> list = checkCodeMapper.selectByMobilePnone(map);

		if (0 < list.size()) {
			CheckCode checkCode = list.get(0);
			long t = DateUtils.getNowDate(DateUtils.DATE_TIME_FORMAT).getTime() - checkCode.getCreateTime().getTime();
			if (CodeTimeOutConstant.timeout < t) {
				//超时
				checkCodeMapper.deleteByPrimaryKey(checkCode.getId());
				return 2;
			}
			if (code.equals(checkCode.getCode())) {
				//匹配上
				checkCodeMapper.deleteByPrimaryKey(checkCode.getId());
				return 1;
			}
			else {
				//匹配不上
				return 0;
			}
		}
		else {
			//数据库没有该手机对应的code
			return 0;
		}

	}

	@Override
	public boolean saveCheckCode(String phone, String code, StringBuffer msg, String sign, int type) throws Exception
	{
		boolean saveRes = false;
		CheckCode checkCode = new CheckCode();
		checkCode.setCode(code);
		checkCode.setType(type);
		int res = checkCodeMapper.insert(checkCode);
		if (res > 0) {
			saveRes = sendMsg(phone, msg, sign);
		}
		return saveRes;
	}

	@Override
	public boolean sendMsg(String mobile, StringBuffer msg, String SIGN) throws Exception
	{
		String rb = SendMsg.sendSms(mobile, msg.toString());
		if (!rb.equals("")) {
			return true;
		}
		else {
			logger.error("短信平台发生异常");
			throw new Exception();
		}
	}

	@Override
	public long sendVerificationCode(String mobile,int type) {
		String code=RandomUtil.getRandomNumber(4);
		//		String PostData = "userid=&account=98a33e&password=kn2t9xmhpz&mobile="+mobile+"&content=您的验证码是"+code+"【校妆网】";
		//		String tr=SendSMS.send(PostData, "http://api.chanzor.com/send");
		//		if (tr.equals("")) {
		//			int i=0;
		//			return i;
		//		}
		SMSVerifyCode sms = new SMSVerifyCode(mobile);
		sms.setCode(code);
		sms.setSync(true);//同步发送
		long verifyCodeLogin = smsService.verifyCodeLogin(sms);
		if (verifyCodeLogin < 1) {
			return verifyCodeLogin;
		}
		int i=checkCodeMapper.insertCheckCode(mobile, code, type);
		if (i<=0) {
			i=0;
			return i;
		}
		i=1;
		return i;
	}

	@Override
	public int selectCheckCodeByMobile(String mobile, String code) {
		// 根据手机号码查询 (按照ID降序排序 limit 1)
		CheckCode checkCode =checkCodeMapper.selectCheckCodeByMobile(mobile);
		if (!checkCode.getCode().equals(code)) {
			int i=0;
			//0不正确
			return i;
		}
		//1正确
		int i=1;
		return i;
	}

	@Override
	public boolean checkStoreUser(String mobile) {
		// TODO Auto-generated method stub
		boolean flag = true;
		User user = userMapper.selectUserByUserNameNopassword(mobile);
		if (user==null || user.getRoleId()<3) {
			flag = false;
		}
		return flag;
	}
	
	
}
